﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace EFong.Contract
{
    [DataContract]
    public class FlightOrderDatail
    {
        private Guid _id = Guid.NewGuid();
        [DataMember]
        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private FlightOrder flightOrder;
        public FlightOrder FlightOrder
        {
            get { return flightOrder; }
            set { flightOrder = value; }
        }

        private string _wayType = string.Empty;
        [DataMember]
        public string WayType
        {
            get { return _wayType; }
            set { _wayType = value; }
        }

        private string _remark = string.Empty;
        [DataMember]
        public string Remark
        {
            get { return _remark; }
            set { _remark = value; }
        }

        private decimal _price;
        [DataMember]
        public decimal Price
        {
            get { return _price; }
            set { _price = value; }
        }

        private decimal _tax1;
        [DataMember]
        public decimal Tax1
        {
            get { return _tax1; }
            set { _tax1 = value; }
        }

        private decimal _tax2;
        [DataMember]
        public decimal Tax2
        {
            get { return _tax2; }
            set { _tax2 = value; }
        }

        private decimal _insuranceFee;
        [DataMember]
        public decimal InsuranceFee
        {
            get { return _insuranceFee; }
            set { _insuranceFee = value; }
        }

        private bool _isInsurancePolicyFree = false;
        [DataMember]
        public bool IsInsurancePolicyFree
        {
            get { return _isInsurancePolicyFree; }
            set { _isInsurancePolicyFree = value; }
        }

        public string InsurancePolicyState
        {
            get 
            {
                if (IsInsurancePolicyFree)
                    return "送";
                else
                    return InsuranceFee.ToString();
            }
        }

        private decimal _discount;
        [DataMember]
        public decimal Discount
        {
            get { return _discount; }
            set { _discount = value; }
        }

        private List<InsurancePolicy> _insurancePolicyList = new List<InsurancePolicy>();
        [DataMember]
        public List<InsurancePolicy> InsurancePolicyList 
        {
            get { return _insurancePolicyList; }
            set { _insurancePolicyList = value; }
        }

        private List<JourneyOrder> _journeyOrderList = new List<JourneyOrder>();
        [DataMember]
        public List<JourneyOrder> JourneyOrderList
        {
            get { return _journeyOrderList; }
            set { _journeyOrderList = value; }
        }
        
        private decimal? _netPrice;
        [DataMember]
        public decimal? NetPrice
        {
            get { return _netPrice; }
            set { _netPrice = value; }
        }

        private decimal? _vendorDiscount;
        [DataMember]
        public decimal? VendorDiscount
        {
            get { return _vendorDiscount; }
            set { _vendorDiscount = value; }
        }

        private decimal? _cost;
        [DataMember]
        public decimal? Cost
        {
            get { return _cost; }
            set { _cost = value; }
        }

        //private decimal _accountReceivable;
        //[DataMember]
        public decimal AccountReceivable
        {
            get
            {
                if (IsInsurancePolicyFree)
                    return Price + Tax1 + Tax2 - Discount;
                else
                    return Price + Tax1 + Tax2 + InsuranceFee - Discount;
            }
            //get { return _accountReceivable; }
            //set { _accountReceivable = value; }
        }

        //private decimal _accountPayable;
        //[DataMember]
        public decimal? AccountPayable
        {
            get
            {
                if (NetPrice != null && NetPrice.Value != 0)
                    return NetPrice.Value + Tax1 + Tax2;
                else if (VendorDiscount != null)
                {
                    decimal temp = Price * (1 - VendorDiscount.Value/100) + Tax1 + Tax2;
                    if (Cost != null)
                        temp += Cost.Value;

                    return temp;
                }
                else
                    return null;
            }
            //get { return _accountPayable; }
            //set { _accountPayable = value; }
        }

        //private decimal _profit;
        //[DataMember]
        public decimal? Profit
        {
            get
            {
                return GetProfit(ExchangeRate);
            }

            //get { return _profit; }
            //set { _profit = value; }
        }

        public decimal? GetProfit(decimal exchangeRate) 
        {
            decimal result = 0;
            //if (FlightOrder == null)
            //    return null;

            //foreach (FlightChange flightChange in FlightChangeHis)
            //{
            //    if (flightChange.Profit == null)
            //        return null;
            //    result += flightChange.Profit.Value;
            //}

            //foreach (FlightReturn flightReturn in FlightReturnHis)
            //{
            //    if (flightReturn.Profit == null)
            //        return null;
            //    result += flightReturn.Profit.Value;
            //}

            if (AccountPayable == null)
                return null;

            result += AccountReceivable - AccountPayable.Value * exchangeRate;
            foreach(InsurancePolicy insurancePolicy in this.InsurancePolicyList)
                result -= insurancePolicy.BasePrice;

            return result;
        }

        public Decimal? CulcIntergral()
        {
            if (FlightOrder == null)
                return null;
            if (FlightOrder.Customer == null)
                return null;
            if (IsInsurancePolicyFree)
                return Price * FlightOrder.Customer.FlightIntergralRate;
            else
                return (Price + InsuranceFee) * FlightOrder.Customer.FlightIntergralRate;
        }

        private decimal _intergral;
        [DataMember]
        public Decimal Intergral
        {
            get { return _intergral; }
            set { _intergral = value; }
        }

        private string _ticketNo = string.Empty;
        [DataMember]
        public string TicketNo
        {
            get { return _ticketNo; }
            set { _ticketNo = value; }
        }

        private string _airwaysOrderNo = string.Empty;
        [DataMember]
        public string AirwaysOrderNo
        {
            get { return _airwaysOrderNo; }
            set { _airwaysOrderNo = value; }
        }

        private string _status = "使用中";
        [DataMember]
        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }

        private Passenger _passenger;
        [DataMember]
        public Passenger Passenger
        {
            get { return _passenger; }
            set { _passenger = value; }
        }

        private Vendor _vendor;
        [DataMember]
        public Vendor Vendor
        {
            get { return _vendor; }
            set { _vendor = value; }
        }

        private Currency _vendorCurrency;
        [DataMember]
        public Currency VendorCurrency
        {
            get { return _vendorCurrency; }
            set { _vendorCurrency = value; }
        }

        private decimal _exchangeRate = 1;
        [DataMember]
        public decimal ExchangeRate
        {
            get { return _exchangeRate; }
            set { _exchangeRate = value; }
        }

        private string _paymentType = string.Empty;
        [DataMember]
        public string PaymentType
        {
            get { return _paymentType; }
            set { _paymentType = value; }
        }

        private BankAccount _bankAccount;
        [DataMember]
        public BankAccount BankAccount
        {
            get { return _bankAccount; }
            set { _bankAccount = value; }
        }

        private List<FlightChange> _flightChangeHis = new List<FlightChange>();
        [DataMember]
        public List<FlightChange> FlightChangeHis
        {
            get { return _flightChangeHis; }
            set { _flightChangeHis = value; }
        }

        private List<FlightReturn> _flightReturnHis = new List<FlightReturn>();
        [DataMember]
        public List<FlightReturn> FlightReturnHis
        {
            get { return _flightReturnHis; }
            set { _flightReturnHis = value; }
        }

        private List<FlightWay> _flightWayList = new List<FlightWay>();
        [DataMember]
        public List<FlightWay> FlightWayList
        {
            get { return _flightWayList; }
            set { _flightWayList = value; }
        }

        private int _seq;
        [DataMember]
        public int Seq
        {
            get { return _seq; }
            set { _seq = value; }
        }

        public string 行程类型 
        {
            get 
            {
                if (this.FlightWayList.Count == 1)
                    return "单程";
                if (this.FlightWayList.Count == 2)
                    return "双程";
                if (this.FlightWayList.Count > 2)
                    return "联程";

                return string.Empty;
            }
        }

        public string 行程 
        {
            get 
            {
                StringBuilder sb = new StringBuilder();
                if (FlightWayList.Count > 0)
                    sb.Append(FlightWayList[0].DepartLocation);
                foreach (FlightWay flightWay in FlightWayList)
                    sb.Append(string.Format(" - {0}", flightWay.Destination));

                return sb.ToString();
            }
        }

        public DateTime? 出发日期 
        {
            get 
            {
                if (FlightWayList.Count > 0)
                    return FlightWayList[0].DepartDate;
                else
                    return null;
            }
        }

        public string FlightNo
        {
            get
            {
                if (FlightWayList.Count > 0)
                    return FlightWayList[0].FlightNo;
                else
                    return null;
            }
        }

        public decimal Tax 
        {
            get 
            {
                return Tax1 + Tax2;
            }
        }

        public string PassengerText
        {
            get
            {
                if (Passenger == null)
                    return string.Empty;

                StringBuilder sb = new StringBuilder();
                if (!string.IsNullOrWhiteSpace(Passenger.CName))
                {
                    sb.Append(Passenger.CName);
                    if (!string.IsNullOrWhiteSpace(Passenger.EName))
                    {
                        sb.Append(string.Format("({0}", Passenger.EName));
                    }

                }
                else
                {
                    sb.Append(Passenger.EName);
                }

                return sb.ToString();
            }
        }

        public override bool Equals(object obj)
        {
            FlightOrderDatail temp = obj as FlightOrderDatail;
            if (temp == null)
                return false;
            return (this.ID.Equals(temp.ID));
        }

    }
}
